//
//  SilenceFolloweeViewController.m
//  Guazi
//
//  Created by koolearn on 11-5-5.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "SilenceFolloweeViewController.h"
#import "User.h"
#import "ContactCell.h"
#import "FriendDetailViewController.h"
#import "UserSearchViewController.h"


@interface SilenceFolloweeViewController(private)  
    - (void) getFolloweeList;
    - (void) didGetFolloweeList:(TwitterClient*)sender object:(NSObject*)object;
    - (User*) findUserInArr:(NSArray*)array userId:(sqlite3_int64)userId;
@end


@implementation SilenceFolloweeViewController

@synthesize users;

#pragma mark - 加载数据

- (void) getFolloweeList
{
    if ( client ) return;
    
    SEL action = @selector(didGetFolloweeList:object:);
    client = [[TwitterClient alloc] initWithTarget:self action:action];
    [client getSilenceFollowings];
}


- (void) didGetFolloweeList:(TwitterClient*)sender object:(NSObject*)object
{
    //client = nil;
    NSMutableArray* result = [NSMutableArray array];
    
    if ( !object && ![object isKindOfClass:[NSArray class]]) return;
    
    NSArray* arr = (NSArray*) object;
    for ( id o in arr ) {
        if ( ![o isKindOfClass:[NSDictionary class]] ) continue;
        NSDictionary* dic = (NSDictionary*) o;
        User* user = [User userWithJsonDictionary:dic];
        [result addObject:user];
    }
    
    self.users = result;
    [self.tableView reloadData];
}


#pragma mark - search

- (void) search
{
    UserSearchViewController* controller = [[UserSearchViewController alloc] initWithDelegate:nil action:nil];
    [self.navigationController pushViewController:controller animated:YES];
    [controller release];
}


#pragma mark - refresh operations

- (void) refreshAddUser:(User*)user
{
    User* theUser = [self findUserInArr:users userId:user.userId];
    if ( !theUser ) {
        [users insertObject:user atIndex:0];
        [self.tableView reloadData];
    }
}


- (void) refreshDeleteUser:(sqlite3_int64)userId
{
    User* user = [self findUserInArr:users userId:userId];
    if ( user ) {
        [users removeObject:user];
        [self.tableView reloadData];
    }
}


#pragma mark - tool operations

- (User*) findUserInArr:(NSArray*)array userId:(sqlite3_int64)userId
{
    if ( !array ) return nil;
    if ( [array count] == 0 ) return nil;
    
    for ( User* user in array ) {
        if ( user.userId == userId ) {
            return user;
        }
    }
    
    return nil;
}



#pragma mark -
#pragma mark Initialization

/*
- (id)initWithStyle:(UITableViewStyle)style {
    // Override initWithStyle: if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
    if ((self = [super initWithStyle:style])) {
    }
    return self;
}
*/


#pragma mark -
#pragma mark View lifecycle

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"默默关注";
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch 
                                                                                          target:self action:@selector(search)] autorelease];

    self.users = [NSMutableArray array];
    [self getFolloweeList];
}


/*
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}
*/
/*
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}
*/
/*
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}
*/
/*
- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}
*/
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/


#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return [users count];
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    static NSString *CellIdentifier = @"SilenceFolloweeCell";
    
    ContactCell *cell = (ContactCell*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[ContactCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    
    User* user = [users objectAtIndex:indexPath.row];
    CGFloat cellHeight = [ContactCell getCellHeight:user isSearching:NO];
    CGRect frame = CGRectMake(LEFT, 1, CELL_WIDTH, cellHeight );
    [cell update:user frame:frame];
    
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    User* user = [users objectAtIndex:indexPath.row];
    CGFloat cellHeight = [ContactCell getCellHeight:user isSearching:NO];
    return cellHeight;
    //return 80;
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/


/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/


/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/


/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/


#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
   User* user = [users objectAtIndex:indexPath.row];
    
    FriendDetailViewController* controller = [[FriendDetailViewController alloc] initWithUser:user];
    [self.navigationController pushViewController:controller animated:YES];
    [controller release];
}


#pragma mark -
#pragma mark Memory management

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Relinquish ownership any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    self.users = nil;
}


- (void)dealloc {
    [users release];
    [super dealloc];
}


@end

